# _*_coding:utf-8_*_
from ATS.instrumentDriver import InstCom as inst


def srcmeas(source, chan):
    # print "chanis  is is is is ",chan
    result = {source: {"Vpp": [], "RiseTime": [], "FallTime": []}}
    # print "source",source
    if chan == 1:
        universalcom({source: ["trig:sour chan1"]})
        universalcom({source: [":TRIG:LEV -2"]})
        rval = universalcom({source: ["meas:vpp? chan1"]}, 3)
        result[source]["Vpp"].append(rval[0])

        rval = universalcom({source: ["meas:ris? chan1"]}, 3)
        result[source]["RiseTime"].append(rval[0])

        rval = universalcom({source: ["meas:fall? chan1"]}, 3)
        result[source]["FallTime"].append(rval[0])

    elif chan == 2:
        universalcom({source: ["trig:sour chan2"]})
        universalcom({source: [":TRIG:LEV -0.2"]})
        rval = universalcom({source: ["meas:vpp? chan2"]}, 3)
        result[source]["Vpp"].append(rval[0])

        rval = universalcom({source: ["meas:ris? chan2"]}, 3)
        result[source]["RiseTime"].append(rval[0])

        rval = universalcom({source: ["meas:fall? chan2"]}, 3)
        result[source]["FallTime"].append(rval[0])

    # print 'srcmeas.result',result
    return result


def universalcom(cmd_dict, func_id=0):
    """
    func_id: 0="write",1="read",2="ask",3="ask for values"
    """
    rval = None
    print('cmd_dict', cmd_dict)
    for s in cmd_dict:
        cmd_str = ";".join(s)
        for cmd_str in cmd_dict[s]:
            if func_id == 0:
                inst.write(s, cmd_str)
            elif func_id == 1:
                rval = inst.read(s)
            elif func_id == 2:
                rval = inst.ask(s, cmd_str)
            elif func_id == 3:
                rval = inst.ask_for_values(s, cmd_str)
            elif func_id == 4:
                rval = inst.read_values(s)
        cmd_dict[s] = []  # clear cmd dict

    print('rval is', rval)
    return rval
